<?php
/**
 * @title 449. 序列化和反序列化二叉搜索树 - 层次遍历
 * @author start2004
 */

class Codec {
    function __construct() {

    }

    /**
     * @param TreeNode $root
     * @return String
     */
    function serialize($root) {
        /**
         * @since 2022-05-11 序列化数组
         */
        $data = [];

        /**
         * @since 2022-05-11 层次遍历
         */
        $nodes = [$root];
        while($nodes){
            $nexts = [];

            /**
             * @since 2022-05-11 处理当前层节点
             */
            foreach ($nodes as $node){
                if($node === null){
                    $data[] = "null";
                } else {
                    $data[] = $node->val;
                    $nexts[] = $node->left;
                    $nexts[] = $node->right;
                }
            }

            /**
             * @since 2022-05-11 下次迭代
             */
            $nodes = $nexts;
        }

        /**
         * @since 2022-05-11 取尾部null
         */
        for($i=count($data)-1; $i>=0; $i--){
            if($data[$i] == "null"){
                unset($data[$i]);
            } else {
                break;
            }
        }

        /**
         * @return
         */
        return implode(",", $data);
    }

    /**
     * @param String $data
     * @return TreeNode
     */
    function deserialize($data) {
        /**
         * @since 2022-05-11 分隔
         */
        $data = explode(",", $data);

        /**
         * @since 2022-05-11 元素数量
         */
        $len = count($data);
        if($len == 0){
            return null;
        } else {}

        /**
         * @since 2022-05-11 层次迭代
         */
        $i = 1;
        $head = $node = new TreeNode($data[0]);
        $nodes = [$node];
        while($nodes){
            $nexts = [];

            foreach ($nodes as $node){
                /**
                 * @since 2022-05-11 左子树
                 */
                if($i<$len){
                    if($data[$i] == "null"){
                    } else {
                        $left = new TreeNode($data[$i]);
                        $node->left = $left;
                        $nexts[] = $left;
                    }
                    $i++;
                } else {}

                /**
                 * @since 2022-05-11 右子树
                 */
                if($i<$len){
                    if($data[$i] == "null"){
                    } else {
                        $right = new TreeNode($data[$i]);
                        $node->right = $right;
                        $nexts[] = $right;
                    }
                    $i++;
                } else {}
            }

            /**
             * @since 2022-05-11 下次迭代
             */
            $nodes = $nexts;
        }

        /**
         * @return
         */
        return $head;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.28_12.php
 */
$datas = [
    [1,2,3,null,4],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new CodeC();
    $result = $obj->deserialize(json_encode($data));
    var_dump($result);
    $result = $obj->serialize($result);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
